import { getMarginFinance, getEquity, getIndexInfo } from '@/services/warning';

export default {
  namespace: 'marketrisk',

  state: {
    marginFinanceData: {},
    equityData: {},
    indexInfoData: {},
  },

  effects: {
    *getMarginFinanceData({ payload }, { call, put }) {
      try {
        const response = yield call(getMarginFinance, payload);
        yield put({
          type: 'setMarginFinance',
          payload: {
            marginFinanceData: response,
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *getEquityData({ payload }, { call, put }) {
      try {
        const response = yield call(getEquity, payload);
        yield put({
          type: 'setEquity',
          payload: {
            equityData: response,
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *getIndexInfoData({ payload }, { call, put }) {
      try {
        const response = yield call(getIndexInfo, payload);
        yield put({
          type: 'setIndexInfo',
          payload: {
            indexInfoData: response,
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
  },
  reducers: {
    setMarginFinance(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setEquity(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    setIndexInfo(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
